<template>
	<view class="resetPassword">
		<view class="tips">若你忘记了密码，可在此重置新密码。</view>
		<view class="form">
			<uni-easyinput type="number" placeholder="请输入手机号" class="input" v-model="username" />
			<uni-easyinput type="password" placeholder="请输入登录密码" class="input" v-model="password" passwordIcon></uni-easyinput>
			<uni-easyinput type="text" placeholder="验证码" class="input">
				<!-- 获取验证码 -->
				<template v-slot:right>
					<view class="btn" @tap="handleGetCode" v-if="!isGetting">获取重置码</view>
					<view class="time" v-else>{{ time }}</view>
				</template>
			</uni-easyinput>
		</view>
		<view class="updateBtn" @tap="handleUpdate">重置密码</view>
	</view>
</template>

<script>
import { resetPassword } from '../../../api/mine/index.js';
export default {
	data() {
		return {
			username: '17596496508',
			password: '123456',
			isGetting: false, // 获取验证码中
			time: 10 // 倒计时时间
		};
	},
	watch: {
		time(newVal, oldVal) {
			// 倒计时为0后,重置time
			if (newVal < 0) {
				this.isGetting = false;
				clearInterval(this.timer);
				this.timer = null;
				this.time = 15;
			}
		}
	},
	methods: {
		// 修改密码
		async handleUpdate() {
			const res = await resetPassword(this.username);
			console.log(res);
			if (res.success) {
				uni.showToast({
					title: '密码修改成功,请重新登录!'
				});
				uni.clearStorage();
				setTimeout(() => {
					uni.navigateTo({
						url: '../../login/login'
					});
				}, 1000);
			}
		},
		// 获取验证码
		handleGetCode() {
			this.isGetting = true;
			this.timer = setInterval(() => {
				this.time--;
			}, 1000);
		}
	}
};
</script>

<style lang="scss" scoped>
.tips {
	color: #999;
	font-size: 28rpx;
	margin-top: 64rpx;
	text-align: center;
}
// 输入框
.form {
	display: flex;
	flex-direction: column;
	padding: 0 70rpx;

	.input {
		width: 100%;
		box-sizing: border-box;
		padding: 32rpx;
		box-shadow: 0 0 60rpx 0 rgba(43, 86, 112, 0.1);
		margin: 24rpx 0;
		border-radius: 60rpx;
		/deep/ .is-input-border {
			min-height: 0 !important;
			border: none;
		}
	}
	.btn {
		font-size: 24rpx;
	}
	.time {
		color: rgba(0, 0, 0, 0.4);
	}
}

.updateBtn {
	width: 600rpx;
	height: 88rpx;
	margin: 200rpx auto 0;
	color: #fff;
	text-align: center;
	line-height: 88rpx;
	background: #1c4b47;
	border-radius: 88rpx;
}
</style>
